<?php

namespace App\Controller\App;

use App\Base\BaseController4App;
use App\Model\GradeCategoryModel;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\RulesModel;
use App\Model\LazyCommon\UserModel;
use App\Model\SubjectModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @route("/app")
 */
class ConfigController extends BaseController4App
{

    /**
     * @route("/getConfig",name="AppGetConfig")
     */
    public function AppGetConfig()
    {
        $CCM = new CCM($this->getDoctrine());
        //获取年级树
        $GradeCategoryModel = new GradeCategoryModel($this->getDoctrine());
        $grade = $GradeCategoryModel->getAllParentSons();
        $data["grade"] = $grade;
        $gradeEntiy = $GradeCategoryModel->getAll();
        $gradeData = $CCM->createSelectFromEntity($gradeEntiy);
        $data["gradeName"] = array_flip($gradeData);

        //获取所有科目
        $SubjectModel = new SubjectModel($this->getDoctrine());
        $subject = $SubjectModel->getAllArray();
        $data["subject"] = $subject;
        $data["subjectName"] = array_flip($CCM->createSelectFromCategoryTree($subject));

        //检查登录
        $uid = $this->isUser();
        if ($uid) {
            $data["uid"] = $uid;
            $data["login"] = 1;
            //如果登录了就设置年级信息
            //年级信息存储到qq字段
            $UserModel = new UserModel($this->getDoctrine());
            $user = $UserModel->getUserById($uid);
            if (!$user) {
                $data["myGrade"] = 0;
                $data["headPic"] = "";
                $data["phone"] = "";
            }else{
                $data["myGrade"] = $user->getQq();
                $data["headPic"] = $user->getHeadPic();
                $data["phone"] = $user->getPhone();
            }
        }else{
            $data["uid"] = 0;
            $data["login"] = 0;
            $data["myGrade"] = 0;
            $data["headPic"] = "";
            $data["phone"] = "";
        }

        //app配置信息
        $RulesModel = new RulesModel($this->getDoctrine());
        $data["qrCode"] = $RulesModel->getByK("KF_QR_CODE")->getV();
        $data["contactPhone"] = $RulesModel->getByK("CONTACT_PHONE")->getV();
        $data["chargeConfig"] = unserialize($RulesModel->getByK("CHARGE_CONFIG")->getV());

        return $this->json_out(1,"OK",$data);
    }

}
